package com.xuan.pmh.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.xuan.pmh.R;
import com.xuan.pmh.bean.Student;
import com.xuan.pmh.util.FinalValue;
import com.xuan.pmh.util.ShareUtil;
import com.xuan.pmh.util.ToastUtil;

public class ManageStudentActivity extends HttpActivity {

	private LinearLayout llShowStudent, llAddStudent;
	private Button btAddStudent, btFinish;
	private List<EditText> list = new ArrayList<EditText>();
	private List<Student> students = new ArrayList<Student>();

	private final int GET_STUDENT = 1;
	private final int MANAGE_STUDENT = 2;

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub
		gsonBean = getGsonBean(result);
		
		if(gsonBean.state == FinalValue.FAIL){
			ToastUtil.show(this, "请填写正确的学号");
			return;
		}
		
		switch (requestCode) {
		case MANAGE_STUDENT:
			if (gsonBean.state == FinalValue.SUCCESS) {
				ToastUtil.show(this, "添加成功");
				paramsList.clear();
				addPostRequest("getManageStudents", paramsList, GET_STUDENT);
				sendPostRequest();
			}
			break;
		case GET_STUDENT:
			llShowStudent.removeAllViews();
			students.clear();
			List<Student> newStudents = gson.fromJson(gsonBean.bean,
					new TypeToken<List<Student>>() {
					}.getType());
			if(newStudents!=null){
				Log.v("newStudent", "不为空");
				students.addAll(newStudents);
			}
			ShareUtil.students = students;
			for (int i = 0; i < students.size(); i++) {
				View ll = inflater.inflate(R.layout.item_user_show, null);
				TextView tvUserId,tvName;
				tvUserId = (TextView)ll.findViewById(R.id.tvUserId);
				tvName = (TextView)ll.findViewById(R.id.tvName);
				tvUserId.setText(students.get(i).userId);
				tvName.setText(students.get(i).name);
				llShowStudent.addView(ll);
				mPrint("llShowStudent添加View");
			}
			break;
		}
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_manage_student;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		btAddStudent = (Button) mFindViewAndSetOnClick(R.id.btAddStudent);
		btFinish = (Button) mFindViewAndSetOnClick(R.id.btFinish);
		llShowStudent = (LinearLayout) findViewById(R.id.llShowStudent);
		llAddStudent = (LinearLayout) findViewById(R.id.llAddStudent);

		paramsList.clear();
		addPostRequest("getManageStudents", paramsList, GET_STUDENT);
		sendPostRequest();

	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.btAddStudent:
			View ll = inflater.inflate(R.layout.item_add_user, null);
			list.add((EditText) ll.findViewById(R.id.etUserId));
			llAddStudent.addView(ll);
			break;
		case R.id.btFinish:
			paramsList.clear();
			List<String> users = new ArrayList<String>();
			for (int i = 0; i < list.size(); i++) {
				users.add(getEditTextString(list.get(i)));
			}
			paramsList.put("param", gson.toJson(users));
			addPostRequest("managerUsers", paramsList, MANAGE_STUDENT);
			sendPostRequest();
			break;
		}
	}

}
